Welcome to DailyCodeHub.
If you are learning Python or preparing for coding interviews. This is one of the most common questions you will come across - swapping two numbers without using a temporary variable. It may look like a very basic problem but it actually plays an important role in building your programming logic.
At first most beginners think this problem is too easy because they already know how to swap values using a third variable. but when the condition is changed to " without using a temporary variable" many people get confused. that is exactly why interviewers like to ask this question.
This type of problem helps them understand how you think, how you handle variables and whether you can apply logic in a simple but effective way.
I personally suggest that instead of just reading the code, you should try each method on your own. When you execute the code step by step, you will understand the concept much more clearly.
In this article we will go through 4 different methods to swap two numbers without using a temporary variable. each method uses a different idea, so by the end of this post, you will have a strong understanding of how swapping works iinternally.
Before we start-let us understand the basics :
Swapping means exchanging the values of two variables.
for example :
a = 5 and b = 10
after swapping :
a = 10 and b = 5
Normally we use third variable like this :
temp = a
a = b
b = temp
But in interviews you are often asked to solve it without using this extra variable. So let us now look at different ways to solve that.
Question :
Write a Python program to swap two integers without using a temporary variable.
Now let us solve this problem in different ways.
Method 1 : Using Arithmetic operators
Code :
# Author: P.Prabhas
# Take input from user
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
# Print values before swapping
print("Before swapping:", a, b)
# Swap without using third variable
a = a + b
b = a - b
a = a - b
# Print values after swapping
print("After swapping:", a, b)
Output :
Enter first number: 5
Enter second number: 10
Before swapping: 5 10
After swapping: 10 5
How it works :
Let us take an example: a = 5 and b = 10
step 1 :
a = a + b → a = 5 + 10 = 15
Now a store both values together
step 2 :
b = a - b → b = 15 - 10 = 5
Now b gets the original value of a
step 3 :
a = a - b → a = 15 - 5 = 10
Now a gets the original value of b
Final result : a = 10, b = 5 (values swapped )
In this method we first combine both values and then separate them step by step using subtraction, so no extra variable is needed.
Method 2: Using the XOR operator
Code :
# Author: P.Prabhas
# Take input from user
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
# Print values before swapping
print("Before swapping:", a, b)
# Swap using XOR operator
a = a ^ b
b = a ^ b
a = a ^ b
# Print values after swapping
print("After swapping:", a, b)
Output :
Enter first number: 5
Enter second number: 10
Before swapping: 5 10
After swapping: 10 5
How it works :
Let's take an example: a = 5 and b = 10
Let us see how values change step by step :
Step1 :
a = a ^ b
Step2 :
b = a ^ b
Step3:
a = a ^ b
Final result: a = 10, b = 5 (values swapped)
In this method XOR combines and restores values. applying XOR twice helps us recover the original values without using extra space.
Method 3 : Using Python Tuple
Code :
# Author: P.Prabhas
# Take input from user
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
# Print values before swapping
print("Before swapping:", a, b)
# Swap using tuple unpacking
a, b = b, a
# Print values after swapping
print("After swapping:", a, b)
Output :
Enter first number: 5
Enter second number: 10
Before swapping: 5 10
After swapping: 10 5
# Author: P.Prabhas
# Take input from user
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
# Print values before swapping
print("Before swapping:", a, b)
# Swap using multiplication and division
a = a * b
b = a // b
a = a // b
# Print values after swapping
print("After swapping:", a, b)
Enter first number: 5
Enter second number: 10
Before swapping: 5 10
After swapping: 10 5
Important note :
- Do not use when one value is 0
- It causes overflow for large values
b = a // b → b = 50 // 10 = 5
a = a // b → a = 50 // 5 = 10

No comments:
Post a Comment